#include <ximera/exception.hpp>
#include <ximera/common/utils.hpp>

#include <sstream>

namespace ximera
{
    Exception::Exception(std::string const& fileName, std::string const& function, int line,
                         std::string const& type, String const& message)
        : _fileName(fileName), 
          _function(function),
          _line(line),
          _type(type), 
          _message(message) 
    {
        _trace.popFront();
    }

    Exception::~Exception()
    {
    }

    String const& Exception::message() const 
    {
        return _message;
    }

    std::string const& Exception::fileName() const 
    {
        return _fileName;
    }

    std::string const& Exception::function() const 
    {
        return _function;
    }

    std::string const& Exception::type() const 
    {
        return _type;
    }

    int Exception::line() const
    {
        return _line;
    }

    StackTrace const& Exception::stackTrace() const
    {
        return _trace;
    }

    char const* Exception::what() const
    {
        if (_whatStr.empty())
        {
            std::wstringstream strm;
            strm << *this;
            _whatStr = string_util::wstr2str(strm.str());
        }

        return _whatStr.c_str();
    }
}